package com.sophialex.health.record.common.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.sophialex.health.common.platform.pojo.MenuInfo;
import com.sophialex.health.system.frame.util.Assert;

/**
 * 读取本地相关文件公共类
 * 
 * @author LP
 * 
 */
public class XmlCommonUtil {
	/*************************************** 菜单 ***************************/
	public static final String Project = "Project";
	public static final String Menu = "Menu";
	public static final String Fbh = "fbh";
	public static final String remotehost = "remotehost";
	public static final String remote_webapp_port = "remote_webapp_port";
	public static final String webcontext = "webcontext";

	/**
	 * 从本地文件读取菜单集
	 * 
	 * @param list
	 */
	@SuppressWarnings("unchecked")
	public static List<MenuInfo> getMenuInfoFromFile() {
		File file = new File(FileConfig.getReportPath()
				+ FileConfig.project_file);
		List<MenuInfo> list = new ArrayList<MenuInfo>();
		if (file == null || !file.exists())
			return loadDefault();
		else {
			SAXReader reader = new SAXReader();
			Document doc;
			Element element;
			MenuInfo info;
			String fbh, mc;
			try {
				doc = reader.read(file);
				Element menu = doc.getRootElement();
				Iterator iterator = menu.elementIterator();
				while (iterator.hasNext()) {
					element = (Element) iterator.next();
					fbh = element.attributeValue(Fbh);
					mc = element.getText();
					if (!Assert.isNull(fbh) && !Assert.isNull(mc)) {
						info = new MenuInfo();
						info.setFmenubh(fbh);
						info.setFmenumc(mc);
						list.add(info);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (list == null || list.size() == 0) {
			return loadDefault();
		}
		return list;
	}

	public static List<MenuInfo> loadDefault() {
		List<MenuInfo> list = new ArrayList<MenuInfo>();

		return list;
	}

	/**
	 * 保存界面下面菜单到文件
	 * 
	 * @param list
	 */
	public static void createMenuFile(List<MenuInfo> list) {
		if (list != null && list.size() > 0) {
			Document doc = DocumentHelper.createDocument();
			Element rootElement = doc.addElement(Project);
			// 设置子节点内容
			for (MenuInfo info : list) {
				Element element = rootElement.addElement(Menu);
				element.addAttribute(Fbh, info.getFmenubh());
				element.setText(info.getFmenumc());
			}
			try {
				File file = new File(FileConfig.getReportPath());
				if (!file.exists() || (file.exists() && !file.isDirectory())) {
					file.mkdirs();
				}
				OutputFormat fmt = new OutputFormat();
				fmt.setEncoding("UTF-8"); //$NON-NLS-1$
				XMLWriter writer = new XMLWriter(fmt);
				// 以输出格式为参数,创建XML文件输出对象
				OutputStream out = new FileOutputStream(FileConfig
						.getReportPath()
						+ "/" + FileConfig.project_file); //$NON-NLS-1$
				// 创建输出流..
				writer.setOutputStream(out);
				// 设置输出流
				writer.write(doc);
				// 输出doc对象,即形成XML文件
				writer.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
